package com.content.view.Frame.MainPanel
{
	import com.content.model.Communication.WebAPIProxy;
	import com.content.model.Const.TransitionType;
	import com.content.model.GlobalVarProxy;
	import com.content.model.LoadedAssets.ConfigProxy;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.AppMediator;
	import com.content.view.Frame.Content.Content;
	import com.library.core.IDisposable;
	
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;
	import flash.events.DataEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class MPBaseMediator extends Mediator implements IMediator, IDisposable
	{
		private var _name:String;
		protected var _view:MPBase;
		
		protected var _isShown:Boolean = false;
		
		/* 由于 Resize 还会在Switch时调用，所以此时发生的Resize并非由尺寸变化导致，如果发现尺寸没有变，则不进行Resize
		 * 此处的 _stageXPrev就是记录上次实质 Resize 时的尺寸，_StageX记录最新的屏幕尺寸以便于核对
		 *------------------------------------- */
		private var _stageWPrev:int = 0;
		private var _stageHPrev:int = 0;
		private var _stageW:int;
		private var _stageH:int;
		
		public function MPBaseMediator(self:MPBaseMediator, name:String, viewComponent:Object=null){
			super(name, viewComponent);
			_name = name;
			
			if(self != this){				
				throw new IllegalOperationError("错误: MPBaseMediator 类不能被实例化！");
			}
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				MPBase.SWITCH,
				App.RESIZE_SYS
			];
		}
		/**
		 * Event Handler
		 */ 
		override public function handleNotification(note:INotification):void
		{
			var name:String = note.getName();
			var body:Object = note.getBody();
			
			switch ( name )
			{
				case MPBase.SWITCH:
					var activeMPName:String = body["MPID"];
					var transitDuration:Number = 0.7;
					Switch(activeMPName, 0, transitDuration);
					break;
				case App.RESIZE_SYS:
					var sWidth:int = body["width"];
					var sHeight:int = body["height"];
					SetLayout(sWidth, sHeight);
					break;
			}
		}
		
		private function SetLayout(sWidth:int, sHeight:int):void{
			// 仅记录最新的尺寸，便于Switch语句中传参数
			_stageW = sWidth;
			_stageH = sHeight;
			
			if(!_isShown) return;	// 当前没有显示，不响应Resize
			if(_stageWPrev == sWidth && _stageHPrev == sHeight) return;	// 尺寸没有变，不响应Resize
			_stageWPrev = sWidth;
			_stageHPrev = sHeight;
			
			_view.SetLayout(sWidth, sHeight);
			_view.x = pConfig.UI_CBarL1_Width + pConfig.UI_CBarL1_MarginLeft + pConfig.UI_MainPanel_MarginLeft;
			_view.y = pConfig.UI_MainPanel_MarginTop;
		}
		
		private function Switch(activeMPName:String, delay:Number, duration:Number):void{
			if(_name == activeMPName){
				// 如果自己成为当前面板
				if(_isShown){
					// 如果已经是当前面板，则忽略
					return;
				}else{
					// 如果还不是当前面板，则变为当前，同时设置尺寸
					_isShown = true;
					SetLayout(_stageW, _stageH);	// 先设置好 _isShown = true,再调用SetLayout，否则会直接 return
					_view.Show(delay, duration);
				}
			}else{
				if(_isShown){
					_isShown = false;
					_view.Hide(0, duration*0.6);
				}else{
					return;
				}
			}
		}
		
		/* Public Method
		 * --------------------------------------------------------------- */
		protected function get ParentView():Sprite{return viewComponent as Sprite;}
		protected function get pWebAPI():WebAPIProxy	{return facade.retrieveProxy( WebAPIProxy.NAME ) as WebAPIProxy;}
		protected function get pConfig():ConfigProxy	{return facade.retrieveProxy( ConfigProxy.NAME ) as ConfigProxy;}
		public function Dispose():void	{
			_view.Dispose();
		}
	}
}